package com.xiong.wuliao.HomeGame;

import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.xiong.wuliao.R;

import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;

public class GameActivity extends AppCompatActivity {

    private CCDirector director;
    private GameLayout layout;
    private Handler handler;
    private PlayerDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 创建画布
        CCGLSurfaceView surfaceView = new CCGLSurfaceView(this);
        setContentView(surfaceView);
        initHandler();
        dao = new PlayerDao(this);

        // 创建导演
        director = CCDirector.sharedDirector();
        // 导演绑定画布
        director.attachInView(surfaceView);
        // 设置游戏屏幕尺寸
        director.setScreenSize(480, 852);
        // 展示刷新帧率
        director.setDisplayFPS(true);
        // 创建场景层对象
        CCScene scene = CCScene.node();
        // 创建一个图层
        layout = new GameLayout(this,handler);
        // 将图层添加到场景中
        scene.addChild(layout);
        // 导演运行场景
        director.runWithScene(scene);
    }

    private void initHandler() {
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                final int score = msg.what;
                View view = null;
                AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
                builder.setTitle("游戏提示");
                view = LayoutInflater.from(GameActivity.this).inflate(R.layout.dialog_item,null);
                final EditText name_editText = (EditText) view.findViewById(R.id.name_edit);
                builder.setView(view);
                builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PlayerBean playerBean = new PlayerBean();
                        String name = name_editText.getText().toString().trim();
                        if (TextUtils.isEmpty(name)){
                            name = "匿名";
                        }
                        playerBean.setName(name);
                        playerBean.setScore(score);
                        dao.add(playerBean);
                        dialog.cancel();
                        finish();
                    }
                });
                builder.show();
            }
        };
    }

    @Override
    protected void onResume() {
        director.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        director.onPause();
        super.onPause();
        SoundEngine.sharedEngine().pauseSound();
    }
}
